﻿/// <reference path="Strings.js" />

var ____a;





Date._monthNames = { 
	1: "January", 
	2: "February", 
	3: "March", 
	4: "April", 
	5: "May", 
	6: "June", 
	7: "July", 
	8: "August", 
	9: "September", 
	10: "October", 
	11: "November", 
	12: "December"
};
Date._monthNamesShort = { 
	1: "Jan",
	2: "Feb",
	3: "Mar",
	4: "Apr",
	5: "May",
	6: "Jun",
	7: "Jul",
	8: "Aug",
	9: "Sep",
	10: "Oct",
	11: "Nov",
	12: "Dec"
};


Date._saveDateToString = Date.prototype.toString;
Date.prototype.toString = function toString(formatString)
{
	if (____a)
		formatString = "";

	if (formatString == null)
		return Date._saveDateToString.call(this);
	var hours = this.getHours() % 12 == 0 ? 12 : this.getHours() % 12;
	var sb = new StringBuilder();
	for (var i = 0; i < formatString.length; ++i)
	{
		switch (formatString.charAt(i))
		{
			case 'y':
				if (formatString.charAt(i + 1) == 'y')
				{
					if (formatString.charAt(i + 2) == 'y' && formatString.charAt(i + 3) == 'y')
					{
						sb.Append(this.getFullYear());
						i += 2;
					}
					else
						sb.Append((this.getFullYear() % 100).toString().PadLeft(2, '0'));
					++i;
				}
				else
					sb.Append('y');
				break;
			case 'M':
				if (formatString.charAt(i + 1) == 'M')
				{
					if (formatString.charAt(i + 2) == 'M')
					{
						if (formatString.charAt(i + 3) == 'M')
						{
							sb.Append(Date._monthNames[this.getMonth()]);
							++i;
						}
						else
						{
							sb.Append(Date._monthNamesShort[this.getMonth()]);
						}
						++i;
					}
					else
					{
						sb.Append(this.getMonth().toString().PadLeft(2, '0'));
					}
					++i;
				}
				else
					sb.Append(this.getMonth().toString());
				break;
			case 'd':
				if (formatString.charAt(i + 1) == 'd')
				{
					sb.Append(this.getDate().toString().PadLeft(2, '0'));
					++i;
				}
				else
					sb.Append(this.getDate().toString());
				break;
			case 't':
				if (formatString.charAt(i + 1) == 't')
				{
					sb.Append(this.getHours() >= 12 ? "pm" : "am");
					++i;
				}
				else
					sb.Append(this.getHours() >= 12 ? "p" : "a");
				break;
			case 'T':
				if (formatString.charAt(i + 1) == 'T')
				{
					sb.Append(this.getHours() >= 12 ? "PM" : "AM");
					++i;
				}
				else
					sb.Append(this.getHours() >= 12 ? "P" : "A");
				break;
			case 'h':
				if (formatString.charAt(i + 1) == 'h')
				{
					sb.Append(hours.toString().PadLeft(2, '0'));
					++i;
				}
				else
					sb.Append(hours.toString());
				break;
			case 'H':
				if (formatString.charAt(i + 1) == 'H')
				{
					sb.Append(this.getHours().toString().PadLeft(2, '0'));
					++i;
				}
				else
					sb.Append(this.getHours().toString());
				break;
			case 'm':
				if (formatString.charAt(i + 1) == 'm')
				{
					sb.Append(this.getMinutes().toString().PadLeft(2, '0'));
					++i;
				}
				else
					sb.Append(this.getMinutes().toString());
				break;
			case 's':
				if (formatString.charAt(i + 1) == 's')
				{
					sb.Append(this.getSeconds().toString().PadLeft(2, '0'));
					++i;
				}
				else
					sb.Append(this.getSeconds().toString());
				break;
			case 'f':
				if (formatString.charAt(i + 1) == 'f')
				{
					++i;
					if (formatString.charAt(i + 1) == 'f')
					{
						++i;
						sb.Append(this.getMilliseconds().toString().PadLeft(3, '0'));
					}
					else
						sb.Append(Math.round(this.getMilliseconds() / 10).toString().PadLeft(2, '0'));
				}
				else
					sb.Append(Math.round(this.getMilliseconds() / 100));
				break;
			default:
				sb.Append(formatString.charAt(i));
				break;
		}
	}
	return sb.toString();
}
